package com.adv.controller.base;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.adv.service.RoleService;
import com.adv.vo.RoleForm;

/**
 * 登录控制器
 * 
 * @author WUQINGLONG
 *
 */
@Controller
public class RoleController extends BaseController {

	@Autowired
	private RoleService service;

	@RequestMapping("/role/list")
	public Object list() {
		return "role/list";
	}

	/**
	 * 获取完整json格式的数据
	 * 
	 * @return json
	 */
	@ResponseBody
	@RequestMapping("/role/json")
	public Object json() {
		return service.getList();
	}

	@RequestMapping("/role/add")
	public String add(RoleForm form, ModelMap model) {
		model.put("role", service.initForm(form));
		return "role/modify";
	}

	@ResponseBody
	@RequestMapping("/role/save")
	public Object save(@Validated RoleForm form, @Valid BindingResult result, ModelMap model) {
		if (result.hasErrors()) {
			// 新建需要校验完整字段
			return convertField2Map(result);
		}
		return service.save(form);
	}

}
